package cn.weforward.task.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import cn.weforward.common.NameItem;
import cn.weforward.common.NameItems;
import cn.weforward.common.ResultPage;
import cn.weforward.data.UniteId;
import cn.weforward.data.annotation.Index;
import cn.weforward.data.log.BusinessLog;
import cn.weforward.data.persister.support.AbstractPersistent;
import cn.weforward.framework.ApiException;
import cn.weforward.framework.support.Global;
import cn.weforward.task.Bug;
import cn.weforward.task.di.TaskDi;

public class SimpleBug extends AbstractPersistent<TaskDi> implements Bug{
	
	@Resource
	String m_Content;
	@Resource
	String m_Severity;
	@Resource
	int m_State;
	@Resource
	List<String> m_Tester = new ArrayList<>();
	@Resource
	List<String> m_Handler= new ArrayList<>();
	@Resource
	String m_Creator;
	@Resource
	Date m_CreateTime;
	@Resource
	String m_VersionAndPlatform;
	@Resource
	Date m_LastHandlerTime;
	@Index
	@Resource
	String m_TaskId;
	
	protected SimpleBug(TaskDi di, String content,String taskId,List<String> handler, 
			String severity,String versionAndPlatForm) {
		//新建一个bug，要有内容、对应任务id、处理人、严重性、版本与平台，而状态、创建人、创建时间自动生成
		super(di);
		genPersistenceId();
		m_Content = content;
		m_Severity = severity;
		m_Handler = handler;
		m_VersionAndPlatform = versionAndPlatForm;
		m_TaskId = taskId;
		m_State = STATE_GONNA_AMEND.id;
		m_Creator = Global.TLS.getValue("user");
		m_CreateTime = new Date();
		
		markPersistenceUpdate();
		getBusinessDi().writeLog(getId(), "新建bug", "新建了一个bug", "无..");
	}
	protected SimpleBug(TaskDi di) {
		super(di);
	}
	
	
	
	
	
	@Override
	public synchronized void gonnaAmend() throws ApiException {
		// 如果当前bug状态为待复测or建议不作修改or申请无法解决，可以扭转为待修正
		if(m_State == STATE_GONNA_RETEST.id || m_State == STATE_ADVICE_NO_UPDATE.id || m_State == STATE_APPLY_CANT_SOLVE.id) {
			int oldState = m_State;
			m_State = STATE_GONNA_AMEND.id;
			markPersistenceUpdate();
			getBusinessDi().writeLog(getId(), "状态扭转", STATES.get(oldState).getName()+" 扭转为 "+STATE_GONNA_AMEND.getName(), "无..");
			refreshLastHandlerTime();
		}else {
			throw new ApiException(10002, "当前状态为 "+STATES.get(m_State).getName()+" 不能扭转到 "+"待修正");
		}
		
	}
	@Override
	public synchronized void gonnaRetest() throws ApiException {
		// 如果当前bug状态为待修正or重新打开，可以扭转为待复测
		if(m_State == STATE_GONNA_AMEND.id || m_State == STATE_REOPEN.id) {
			int oldState = m_State;
			m_State = STATE_GONNA_RETEST.id;
			markPersistenceUpdate();
			getBusinessDi().writeLog(getId(), "状态扭转", STATES.get(oldState).getName()+" 扭转为 "+STATE_GONNA_RETEST.getName(), "无..");
			refreshLastHandlerTime();
		}else {
			throw new ApiException(10002, "当前状态为 "+STATES.get(m_State).getName()+" 不能扭转到 "+"待复测");
		}
		
	}
	@Override
	public synchronized void adviceNoUpdate() throws ApiException {
		// 如果当前bug状态为待修正，可以扭转为不作修改
		if(m_State == STATE_GONNA_AMEND.id) {
			int oldState = m_State;
			m_State = STATE_ADVICE_NO_UPDATE.id;
			markPersistenceUpdate();
			getBusinessDi().writeLog(getId(), "状态扭转", STATES.get(oldState).getName()+" 扭转为 "+STATE_ADVICE_NO_UPDATE.getName(), "无..");
			refreshLastHandlerTime();
		}else {
			throw new ApiException(10002, "当前状态为 "+STATES.get(m_State).getName()+" 不能扭转到 "+"不作修改");
		}
		
	}
	@Override
	public synchronized void applyCantSolve() throws ApiException {
		// 如果当前bug状态为待修正，可以扭转为申请无法解决
		if(m_State == STATE_GONNA_AMEND.id) {
			int oldState = m_State;
			m_State = STATE_APPLY_CANT_SOLVE.id;
			markPersistenceUpdate();
			getBusinessDi().writeLog(getId(), "状态扭转", STATES.get(oldState).getName()+" 扭转为 "+STATE_APPLY_CANT_SOLVE.getName(), "无..");
			refreshLastHandlerTime();
		}else {
			throw new ApiException(10002, "当前状态为 "+STATES.get(m_State).getName()+" 不能扭转到 "+"申请无法解决");
		}
		
	}
	@Override
	public synchronized void solve() throws ApiException {
		// 如果当前bug状态为待复测，可以扭转为已解决
		if(m_State == STATE_GONNA_RETEST.id) {
			int oldState = m_State;
			m_State = STATE_SOLVE.id;
			markPersistenceUpdate();
			getBusinessDi().writeLog(getId(), "状态扭转", STATES.get(oldState).getName()+" 扭转为 "+STATE_SOLVE.getName(), "无..");
			refreshLastHandlerTime();
		}else {
			throw new ApiException(10002, "当前状态为 "+STATES.get(m_State).getName()+" 不能扭转到 "+"已解决");
		}
		
	}
	@Override
	public synchronized void noUpdate() throws ApiException {
		// 如果当前bug状态为建议不作修改，可以扭转为不作修改
		if(m_State == STATE_ADVICE_NO_UPDATE.id) {
			int oldState = m_State;
			m_State = STATE_NO_UPDATE.id;
			markPersistenceUpdate();
			getBusinessDi().writeLog(getId(), "状态扭转", STATES.get(oldState).getName()+" 扭转为 "+STATE_NO_UPDATE.getName(), "无..");
			refreshLastHandlerTime();
		}else {
			throw new ApiException(10002, "当前状态为 "+STATES.get(m_State).getName()+" 不能扭转到 "+"不作修改");
		}
		
	}
	@Override
	public synchronized void cantSolve() throws ApiException {
		// 如果当前bug状态为申请无法解决，可以扭转为无法解决
		if(m_State == STATE_APPLY_CANT_SOLVE.id) {
			int oldState = m_State;
			m_State = STATE_CANT_SOLVE.id;
			markPersistenceUpdate();
			getBusinessDi().writeLog(getId(), "状态扭转", STATES.get(oldState).getName()+" 扭转为 "+STATE_CANT_SOLVE.getName(), "无..");
			refreshLastHandlerTime();
		}else {
			throw new ApiException(10002, "当前状态为 "+STATES.get(m_State).getName()+" 不能扭转到 "+"无法解决");
		}
		
	}
	@Override
	public synchronized void reOpen() throws ApiException {
		// 如果当前bug状态为已解决or不作修改or无法解决，可以扭转为重新打开
		if(m_State == STATE_SOLVE.id || m_State == STATE_NO_UPDATE.id || m_State == STATE_CANT_SOLVE.id) {
			int oldState = m_State;
			m_State = STATE_REOPEN.id;
			markPersistenceUpdate();
			getBusinessDi().writeLog(getId(), "状态扭转", STATES.get(oldState).getName()+" 扭转为 "+STATE_REOPEN.getName(), "无..");
			refreshLastHandlerTime();
		}else {
			throw new ApiException(10002, "当前状态为 "+STATES.get(m_State).getName()+" 不能扭转到 "+"重新打开");
		}
		
	}
	@Override
	public void setContent(String content) {
		String oldContent = m_Content;
		if(m_Content.equals(content)) {
			return ;
		}
		m_Content = content;
		markPersistenceUpdate();
		getBusinessDi().writeLog(getId(), "修改bug内容", oldContent+" 修改为 "+content, "无..");
		refreshLastHandlerTime();
	}
	@Override
	public void setTester(String testeres) {
		List<String> oldTesteres = m_Tester;
		if(m_Tester.contains(testeres)) {
			return ;
		}
		m_Tester.add(testeres);
		markPersistenceUpdate();
		getBusinessDi().writeLog(getId(), "更新bug测试人", oldTesteres+" 修改为 "+m_Tester, "无..");
		refreshLastHandlerTime();
	}
	@Override
	public void setHandler(String handler) {
		List<String> oldHandler = m_Handler;
		//如果原来的处理人中包含要新添加的处理人，则不添加
		if(m_Handler.contains(handler)) {
			return;
		}
		m_Handler.add(handler);
		markPersistenceUpdate();
		getBusinessDi().writeLog(getId(), "更新bug处理人", oldHandler+" 修改为 "+m_Handler, "无..");
		refreshLastHandlerTime();
	}

	@Override
	public void setSeverity(String severity) {
		String oldSeverity = m_Severity;
		if(m_Severity.equals(severity)) {
			return;
		}
		m_Severity = severity;
		markPersistenceUpdate();
		getBusinessDi().writeLog(getId(), "修改bug严重性", oldSeverity+" 修改为 "+severity, "无..");
		refreshLastHandlerTime();
	}
	@Override
	public void setVersionAndPlatform(String versionAndPlatform) {
		String oldVersionAndPlatform = versionAndPlatform;
		if(m_VersionAndPlatform.equals(versionAndPlatform)) {
			return;
		}
		m_VersionAndPlatform = versionAndPlatform;
		markPersistenceUpdate();
		getBusinessDi().writeLog(getId(), "修改bug版本与平台", oldVersionAndPlatform+" 修改为 "+versionAndPlatform, "无..");
		refreshLastHandlerTime();
	}
	
	
	
	public Date getLastHandlerTime() {
		return m_LastHandlerTime;
	}
	public void refreshLastHandlerTime() {
//		Date oldLastHandlerTime = m_LastHandlerTime;
		m_LastHandlerTime = new Date();
		markPersistenceUpdate();
//		getBusinessDi().writeLog(getId(), "bug最后处理时间变化", oldLastHandlerTime+" 变化为 "+m_LastHandlerTime, "无..");
	}
	@Override
	public ResultPage<BusinessLog> getLogs() {
		return getBusinessDi().getLogs(getId());
	}
	@Override
	public String getContent() {
		return m_Content;
	}
	@Override
	public List<String> getTester() {
		return m_Tester;
	}
	@Override
	public List<String> getHandler() {
		return m_Handler;
	}
	@Override
	public String getSeverity() {
		return m_Severity;
	}
	@Override
	public String getVersionAndPlatform() {
		return m_VersionAndPlatform;
	}
	@Override
	public UniteId getId() {
		return getPersistenceId();
	}
	@Override
	public String getTaskId() {
		// TODO Auto-generated method stub
		return m_TaskId;
	}
	@Override
	public NameItem getState() {
		// TODO Auto-generated method stub
		return STATES.get(m_State);
	}
	@Override
	public String getCreator() {
		// TODO Auto-generated method stub
		return m_Creator;
	}
	

}
